package com.example.tech.folio.web;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import lombok.extern.slf4j.Slf4j;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    public JsonResult handleRuntimeException(RuntimeException e) {
        log.error("捕获到运行时异常: {}", e.getMessage());
        switch (e.getMessage()) {
            case State.ERR_UNAUTHORIZED_USERNAME:
                return JsonResult.fail(40101, "认证失败，用户名不存在");
            case State.ERR_UNAUTHORIZED_PASSWORD:
                return JsonResult.fail(40102, "认证失败，密码错误");
            case State.ERR_NOT_FOUND:
                return JsonResult.fail(40400, "尝试访问的数据不存在");
            case State.ERR_CONFLICT:
                return JsonResult.fail(40900, "参数有误");
            default:
                return JsonResult.fail(50000, "未知错误");
        }
    }
}